
$(document).ready(init);
function init() {
	$("#blocks_area tr").bind("mouseover",function() {
		$(this).css("background","#e8f5fe");
	});
	$("#blocks_area tr").bind("mouseout",function() {
		$(this).css("background","#fffcfc");
	});
	$(":input[@name=edit_block]").bind("click",function(){
		//展示出修改表单
		var bid = $(this).parent().parent().find("input:checkbox").val();
		var blockname = $(this).parent().parent().find(".blockname").text();
		var blockdesc = $(this).parent().parent().find(".blockdesc").text();
		var block_code = code_new_block(0,blockname,blockdesc,bid);
		$("#blocks_area form").remove();
		$("#blocks_area").append($(block_code));
		$("#create_new_block").bind("click",function() {
			ajax_create_new_block();
		});
	});
	$(":input[@name=del_block]").bind("click",function(){
		var delblockflag = window.confirm("您确认要删除该版块吗？");
		if(delblockflag) {
			$.post(
			"?controller=JRBlocks&action=DeleteBlock",
				{id: $(this).parent().parent().find("input:checkbox").val()},
				control_blocks
			);
		}
	});
	$("#new_block").bind("click",function() {
		var block_code = code_new_block(0,'','',0);
		$("#blocks_area form").remove();
		$("#blocks_area").append($(block_code));
		$("#create_new_block").bind("click",function() {
			ajax_create_new_block();
		});
	});
}

	function code_new_block(parent,name,desc,id) {
		var name = typeof name=="undefined"?'':name;
		var desc = typeof desc=="undefined"?'':desc;
		var code ="<form id=\"form_new_block\">";
		code +="<div id=\"new_block\" style=\"text-align:left;\">";
		code +="<p>";
		code +="<label class=\"\">名称</label><input type=\"text\" name=\"blockname\" id=\"blockname\" value=\""+name+"\" />";
		code +="</p>";
		code +="<p>";
		code +="<label class=\"\">描述</label><textarea name=\"blockdesc\" id=\"blockdesc\">"+desc+"</textarea>";
		code +="</p>";
		code +="<p>";
		code +="<input type=\"hidden\" name=\"id\" id=\"id\" value=\""+id+"\" />";
		code +="<input type=\"hidden\" name=\"parent\" id=\"parent\" value=\""+parent+"\" />";
		code +="<input type=\"button\" value=\"创建\" id=\"create_new_block\" name=\"create_new_block\" />";
		code +="</p>";
		code +="</div>";
		code +="</form>";
		return code;
	}
	function ajax_create_new_block() {
		$.post(
			"?controller=JRBlocks&action=CreateBlock",
			{parent:$("#parent").val(),blockname:$("#blockname").val(),blockdesc:$("#blockdesc").val(),id:$("#id").val()},
			control_blocks
		);
	}
	function control_blocks() {
		//替换content内容为版块设置内容,功能:crud
		$.post(
			"?controller=JRBlocks",function(htmlcode){
			$("#blocks_area").html(htmlcode);
			init();
		});
	}
var jiraa_blocks = 1;